#!/bin/python
#-*- coding:utf-8 -*-
# Filename:    main.py
# Revision:    1.0
# Date:        2012-06-14
# Author:      simonzhang
# web:         www.simonzhang.net
# Email:       simon-zzm@163.com
### END INIT INFO
import sys
import os
import tornado.ioloop
import tornado.locale
import tornado.web
from dbmodel import *
from interface import *


class GetDelPackageHandler(BaseHandler):
    @tornado.web.authenticated
    def get(self):
        _get_user = self.check_user()
        _get_project_name = self.get_argument('s')
        if u"%s" % _get_project_name == u"选择项目":
            self.write(u"别瞎操作")
            return
        _get_project_package=get_project_package_name('%s' % _get_project_name)
        self.render("templates/getdelpackage.html", props=_get_project_package)



class DelPackageHandler(BaseHandler):
    @tornado.web.authenticated
    def get(self):
        _get_user = self.check_user()
        _domain, _title = if_get_domain_title()
        _get_user_left_menu = if_get_user_left_menu(_get_user)
        _get_user_manage_project = if_get_user_project(_get_user)
        self.render("templates/del_package.html", domain=_domain, \
                                                  title=_title, \
                                                  username = _get_user, \
                                                  items=_get_user_left_menu, \
                                                  pros=_get_user_manage_project)
    def post(self):
        _get_user = self.check_user()
        _domain, _title = if_get_domain_title()
        _get_user_left_menu = if_get_user_left_menu(_get_user)
        _get_user_manage_project = if_get_user_project(_get_user)
        _get_project_package = self.get_arguments('CheckboxGroup1')
        for i in _get_project_package:
            try:
                os.system("/bin/rm -rf project/%s" % i)
            except:
                print "del %s package history error!" % i
            try:
                os.system("/bin/rm -rf project/%s_bak" % i)
            except:
                print "del %s_bak package history error!" % i
            _get_project_name=i.split("_")[0]
            _get_project_date=i.split("_")[1]
            log_file_path = odb_deploy_log_list(_get_project_name, \
                                                _get_project_date)
            try:
                os.remove('tmplog/%s.log' % i)
            except:
                pass
            try:
                idb_del_package_history(_get_project_name, _get_project_date)
            except:
                print "del package history mysql  error"
        self.render("templates/del_package.html", domain=_domain, \
                                                  title=_title, \
                                                  username = _get_user, \
                                                  items=_get_user_left_menu, \
                                                  pros=_get_user_manage_project)
